// 演示数据生成器
// 用于测试 DeepSeek API 动态界面生成功能

const DemoDataGenerator = {
    // 生成销售数据
    generateSalesData() {
        return [
            { name: "总销售额", value: "¥2,458,000" },
            { name: "本月增长", value: "+15.3%" },
            { name: "订单数量", value: "1,234" },
            { name: "客户数量", value: "856" },
            { name: "平均订单价值", value: "¥1,992" }
        ];
    },

    // 生成用户数据
    generateUserData() {
        return [
            { name: "活跃用户", value: "45,672" },
            { name: "新增用户", value: "2,843" },
            { name: "用户留存率", value: "78.5%" },
            { name: "平均会话时长", value: "12分38秒" },
            { name: "跳出率", value: "32.1%" }
        ];
    },

    // 生成财务数据
    generateFinanceData() {
        return [
            { name: "总收入", value: "¥8,943,500" },
            { name: "总支出", value: "¥6,234,200" },
            { name: "净利润", value: "¥2,709,300" },
            { name: "利润率", value: "30.3%" },
            { name: "现金流", value: "¥4,523,100" }
        ];
    },

    // 生成产品数据
    generateProductData() {
        return [
            { name: "产品A销量", value: "15,420" },
            { name: "产品B销量", value: "12,856" },
            { name: "产品C销量", value: "9,734" },
            { name: "最佳表现产品", value: "产品A" },
            { name: "库存预警", value: "3个产品" }
        ];
    },

    // 生成系统监控数据
    generateSystemData() {
        return [
            { name: "服务器状态", value: "正常" },
            { name: "CPU使用率", value: "68.5%" },
            { name: "内存使用率", value: "72.3%" },
            { name: "磁盘使用率", value: "45.8%" },
            { name: "网络延迟", value: "23ms" },
            { name: "在线用户", value: "2,847" }
        ];
    },

    // 生成市场分析数据
    generateMarketData() {
        return [
            { name: "市场份额", value: "12.5%" },
            { name: "竞争对手数量", value: "8" },
            { name: "品牌知名度", value: "76.2%" },
            { name: "客户满意度", value: "4.6/5.0" },
            { name: "推荐指数", value: "8.3/10" }
        ];
    },

    // 生成随机数据
    generateRandomData() {
        const dataTypes = [
            this.generateSalesData,
            this.generateUserData,
            this.generateFinanceData,
            this.generateProductData,
            this.generateSystemData,
            this.generateMarketData
        ];
        
        const randomType = dataTypes[Math.floor(Math.random() * dataTypes.length)];
        return randomType.call(this);
    },

    // 生成复杂的嵌套数据
    generateComplexData() {
        return {
            title: "企业综合数据报告",
            timestamp: new Date().toISOString(),
            summary: {
                totalRevenue: "¥12,456,789",
                totalUsers: 98765,
                growthRate: "+23.4%"
            },
            departments: [
                {
                    name: "销售部",
                    data: this.generateSalesData()
                },
                {
                    name: "市场部",
                    data: this.generateMarketData()
                },
                {
                    name: "技术部",
                    data: this.generateSystemData()
                }
            ],
            trends: [
                { period: "Q1", value: 234000 },
                { period: "Q2", value: 456000 },
                { period: "Q3", value: 678000 },
                { period: "Q4", value: 892000 }
            ]
        };
    },

    // 模拟 JSON 响应格式
    generateJsonResponse(dataType = 'sales') {
        let data;
        
        switch(dataType) {
            case 'sales':
                data = this.generateSalesData();
                break;
            case 'users':
                data = this.generateUserData();
                break;
            case 'finance':
                data = this.generateFinanceData();
                break;
            case 'products':
                data = this.generateProductData();
                break;
            case 'system':
                data = this.generateSystemData();
                break;
            case 'market':
                data = this.generateMarketData();
                break;
            case 'complex':
                return this.generateComplexData();
            default:
                data = this.generateRandomData();
        }

        return {
            requestedData: data,
            timestamp: new Date().toISOString(),
            status: "success",
            message: "数据获取成功"
        };
    }
};

// 工具函数：模拟聊天机器人返回包含 JSON 的消息
function generateDemoMessage(dataType = 'sales') {
    const jsonData = DemoDataGenerator.generateJsonResponse(dataType);
    const jsonString = JSON.stringify(jsonData, null, 2);
    
    const messages = [
        "根据您的请求，我为您整理了以下数据：",
        "这里是您要查看的数据分析结果：",
        "以下是系统生成的数据报告：",
        "数据查询完成，结果如下：",
        "为您提供最新的数据统计："
    ];
    
    const randomMessage = messages[Math.floor(Math.random() * messages.length)];
    
    return `${randomMessage}\n\n\`\`\`json\n${jsonString}\n\`\`\`\n\n希望这些数据对您有帮助！`;
}

// 测试函数：在浏览器控制台中使用
function testDemoData() {
    console.log("=== 演示数据测试 ===");
    
    console.log("1. 销售数据：", DemoDataGenerator.generateSalesData());
    console.log("2. 用户数据：", DemoDataGenerator.generateUserData());
    console.log("3. 财务数据：", DemoDataGenerator.generateFinanceData());
    console.log("4. 复杂数据：", DemoDataGenerator.generateComplexData());
    
    console.log("5. 聊天消息示例：");
    console.log(generateDemoMessage('sales'));
}

// 快速测试按钮：添加到页面进行测试
function addDemoButtons() {
    if (typeof document === 'undefined') return;
    
    const demoContainer = document.createElement('div');
    demoContainer.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 9999;
        background: rgba(255,255,255,0.9);
        padding: 15px;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        border: 1px solid #ddd;
    `;
    
    const title = document.createElement('h4');
    title.textContent = '演示数据测试';
    title.style.margin = '0 0 10px 0';
    demoContainer.appendChild(title);
    
    const dataTypes = [
        { key: 'sales', label: '销售数据' },
        { key: 'users', label: '用户数据' },
        { key: 'finance', label: '财务数据' },
        { key: 'products', label: '产品数据' },
        { key: 'system', label: '系统数据' },
        { key: 'complex', label: '复杂数据' }
    ];
    
    dataTypes.forEach(type => {
        const btn = document.createElement('button');
        btn.textContent = type.label;
        btn.style.cssText = `
            display: block;
            width: 100%;
            margin: 5px 0;
            padding: 8px 12px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
        `;
        
        btn.onclick = () => {
            if (window.vueApp) {
                // 模拟接收到包含 JSON 数据的消息
                const data = DemoDataGenerator.generateJsonResponse(type.key);
                window.vueApp.dataArray = data.requestedData || [data];
                
                // 添加到聊天历史
                window.vueApp.chatHistory.push({
                    content: generateDemoMessage(type.key),
                    isUser: false,
                    isDemo: true
                });
                
                console.log(`生成了${type.label}:`, data);
            } else {
                console.error('Vue应用未找到');
            }
        };
        
        demoContainer.appendChild(btn);
    });
    
    // 添加关闭按钮
    const closeBtn = document.createElement('button');
    closeBtn.textContent = '关闭';
    closeBtn.style.cssText = `
        position: absolute;
        top: 5px;
        right: 5px;
        background: #ff4757;
        color: white;
        border: none;
        border-radius: 50%;
        width: 20px;
        height: 20px;
        cursor: pointer;
        font-size: 12px;
    `;
    closeBtn.onclick = () => document.body.removeChild(demoContainer);
    demoContainer.appendChild(closeBtn);
    
    document.body.appendChild(demoContainer);
}

// 如果在浏览器环境中，自动添加到全局对象
if (typeof window !== 'undefined') {
    window.DemoDataGenerator = DemoDataGenerator;
    window.generateDemoMessage = generateDemoMessage;
    window.testDemoData = testDemoData;
    window.addDemoButtons = addDemoButtons;
    
    // 自动添加演示按钮（可选）
    // document.addEventListener('DOMContentLoaded', addDemoButtons);
} 